﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using PRET.ReportDataDefine.UIData;
using PRET.ReportControlLib.ServiceReference;

namespace PRET.ReportControlLib.ReportView
{
    public partial class TeamEfficiencyReportView : UserControl
    {
        private PRETWebServiceClient adapter = null;

        public TeamEfficiencyReportView()
        {
            InitializeComponent();
            adapter = new PRETWebServiceClient();
            this.setYearPanel.showReportEvent += setYearPanel_showReportEvent;
        }

        public void SetEfficiencyQuota()
        {
            Dictionary<string, int> quota = this.adapter.GetEfficiencyQuota(2);
            this.setYearPanel.SetEfficiencyList(quota);
        }

        void setYearPanel_showReportEvent(object sender, Controls.ShowYearReportViewEventArgs args)
        {
            try
            {
                this.setYearPanel.EnableControls(false);

                if (this.adapter == null)
                {
                    MessageBox.Show("TeamEfficiencyReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.teamEfficiencyReportViewer.LocalReport.DataSources.Clear();
                // 从后台获得班组效率数据
                TeamEfficiencyReport[] list = this.adapter.GetTeamEfficiencyReport(args.StartYear, args.Quota);
                if (list != null && list.Length > 0)
                {
                    Microsoft.Reporting.WinForms.ReportDataSource dataset = new Microsoft.Reporting.WinForms.ReportDataSource("TeamEfficiencyReportDS");
                    this.teamEfficiencyReportViewer.LocalReport.DataSources.Add(dataset);
                    dataset.Value = list.ToList<TeamEfficiencyReport>();
                }
                this.teamEfficiencyReportViewer.LocalReport.Refresh();
                this.teamEfficiencyReportViewer.RefreshReport();
            }
            catch (Exception ex)
            {
                string msg = string.Format("TeamEfficiencyReportView.setYearPanel_showReportEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.setYearPanel.EnableControls(true); }
        }

        //public void ShowReportView()
        //{
        //    if (this.adapter == null)
        //    {
        //        MessageBox.Show("TeamEfficiencyReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return;
        //    }

        //    if (!this.setYearPanel.IsCorrectYear())
        //    {
        //        MessageBox.Show("请从列表中选择正确的年份！", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return;
        //    }

        //    this.teamEfficiencyReportViewer.LocalReport.DataSources.Clear();
        //    // 从后台获得班组效率数据
        //    TeamEfficiencyReport[] list = this.adapter.GetTeamEfficiencyReport(this.setYearPanel.GetSelectedYear);
        //    if (list != null && list.Length > 0)
        //    {
        //        Microsoft.Reporting.WinForms.ReportDataSource dataset = new Microsoft.Reporting.WinForms.ReportDataSource("TeamEfficiencyReportDS");
        //        this.teamEfficiencyReportViewer.LocalReport.DataSources.Add(dataset);
        //        dataset.Value = list.ToList<TeamEfficiencyReport>();
        //    }
        //    this.teamEfficiencyReportViewer.LocalReport.Refresh();
        //    this.teamEfficiencyReportViewer.RefreshReport();
        //}
    }
}
